<?php

namespace frontend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
/**
 * This is the model class for table "ProjectClass".
 *
 */
class ProjectClass extends ActiveRecord
{
    public $type;//操作类型

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project_class}}';
    }



    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title','info','status'], 'trim'],
            [['id','title','status'], 'required', 'message'=>'{attribute}不能为空'],
            [['id','type'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'=>'项目分类id',
            'title'=>'标题',
            'info'=>'简介',
            'status'=>'项目分类状态',
            'type'=>'操作类型',
        ];
    }



    public function scenarios()
    {
        return [
            'default' => [
                'title',
                'info',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
                'type',
            ],
            'update_status' => [
                'id',
                'type',
            ],
            'create' => [
                'title',
                'info',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update' => [
                'id',
                'title',
                'info',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
        ];
    }



    /**
     * 获取项目分类列表
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectClassList($params){
        if(isset($params['status']) && $params['status']!='' && !empty($params['title'])){
            $condition= ['and','status='.intval($params['status']),['like','title',$params['title']],['deleted_at'=>null]];
        }elseif( isset($params['status']) && $params['status']!=''){
            $condition['status']=intval($params['status']);
        }elseif(!empty($params['title'])){
            $condition = ['and',['like','title',$params['title']],['deleted_at'=>null]];
        }else{
            $condition = ['deleted_at'=>null];
        }
        $list = (new Query())
            ->select('id,title,status')
            ->from('project_class')
            ->where($condition)
            ->all();
        return empty($list)?array():$list;

    }





}
